import sys

import requests
import json


# 发送get请求
def getOp():
    # 不带参数的请求
    url = "http://www.baidu.com"
    res = requests.get(url)
    print(res.content.decode("UTF-8"))

    # 带参数的请求 - 将参数放在URL中进行请求
    res = requests.get("http://httpbin.org/get?name=gemey&age=22")
    print(res.text)

    # 带参数的请求 - 将参数写在字段中进行请求
    params = {
        "name": "刘鹏",
        "age": 20
    }
    url = "http://httpbin.org/get"
    res = requests.get(url=url, params=params)
    print(res.text)

    # 自定义HTTP头信息进行请求
    params = {
        "name": "刘鹏",
        "age": 20
    }
    url = 'http://httpbin.org/get'
    headers = {
        "User-agent": "Mozilla/5.0 (Linux; Android 8.1.0; SM-P585Y) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36",
        "referer": "https://www.abidu.com",
        "Cookies": "1234565678"
    }
    res = requests.get(url=url, params=params, headers=headers)
    print(res.text)


# 发送post请求
def postOp():
    # 将参数name和age定义到字典params中
    params = {
        "name": "tony",
        "age": 20
    }
    url = 'http://httpbin.org/post'
    headers = {
        "User-agent": "Mozilla/5.0 (Linux; Android 8.1.0; SM-P585Y) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36",
        "referer": "https://www.abidu.com",
        "Cookies": "username=user1&passwd=passwd"
    }
    # cookie可以以字典的形式赋值给cookies参数
    cookies = {
        "name": "mars"
    }
    # 通过RequestsCookieJar对象可以设置cookie的域、path等信息（http://httpbin.org/cookies可以返回cookie信息）
    jar = requests.cookies.RequestsCookieJar()
    jar.set('username', 'mars', domain='httpbin.org', path='/cookies')
    jar.set('password', 'loo', domain='httpbin.org', path='/else')

    # 填充请求体
    d = {
        "mars": "loo"
    }
    headers["content-type"] = "application/json"

    # 文件
    # files = {
    #     'image': open('111_python.png', 'rb'),
    # }
    # 参数有files的时候，data不能为string
    # res = requests.post(url=url, params=params, headers=headers, cookies=jar, data=json.dumps(d), files=files)
    with open('111_python.png', 'rb') as f:
        requests.post(url=url, data=f)

    # 发起请求
    res = requests.post(url=url, params=params, headers=headers, cookies=jar, data=json.dumps(d))
    print(res.text)


# 读取内容
def readOp():
    # 以content方式读取
    url = "http://www.baidu.com"
    res = requests.get(url)
    # 不进行解码，直接输出
    print(res.content)
    # 进行解码输出
    print(res.content.decode("UTF-8"))

    # 以text方式读取
    url = "http://www.baidu.com"
    res = requests.get(url)
    print(res.text)

    # 读取json数据
    params = {
        "name": "tony",
        "age": 20
    }
    url = 'http://httpbin.org/get'
    headers = {
        "referer": "https://www.abidu.com",
        "Cookies": "1234565678"
    }
    res = requests.get(url=url, params=params, headers=headers)
    print("-----------")
    print(res.json())

    # 读取cookie
    url = 'https://www.baidu.com'
    res = requests.get(url=url)
    print("###########")
    print(res.cookies.extract_cookies)

    # 读取状态码
    print(res.raise_for_status())
    # 读取编码格式
    print(res.encoding)


def otherOp():
    # 设置超时退出
    url = 'https://www.baidu.com'
    try:
        # 设置超时上限时间为1秒
        res = requests.get(url=url, timeout=1)
        print(res.content.decode("UTF-8"))
    except TimeoutError as e:
        print(e)

    # 设置代理
    # url = 'https://www.baidu.com'
    # proxies = {
    #     'http': 'http://123.45.67.76:8888',
    #     'https': 'http://123.45.67.76:4433'
    # }
    # try:
    #     res = requests.get(url=url, timeout=1, proxies=proxies)
    #     print(res.content.decode("UTF-8"))
    # except TimeoutError as e:
    #     print(e)

    # 根据响应获取请求
    url = 'https://www.baidu.com'
    headers = {
        "referer": "https://www.abidu.com",
        "Cookies": "1234565678"
    }
    res = requests.get(url, headers=headers)
    print(res.request.headers)


if __name__ == '__main__':
    # 找到python安装路径Scripts文件下的pip.exe（pip是专门安装库的一种工具）
    # /Users/liupeng/learn/python3.12/python_django/venv/bin/python
    pythonPath = sys.executable
    print(pythonPath)

    otherOp()
